package com.forum.impl.talk;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.common.entity.ApiResult;
import com.common.utils.LocalContext;
import com.common.vo.*;
import com.forum.mapper.forum.ForumUserMapper;
import com.forum.mapper.talk.TalkPostsCommentMapper;
import com.forum.mapper.talk.TalkPostsCommentReplyMapper;
import com.forum.mapper.talk.TalkPostsMapper;
import com.forum.param.PostsQueryParam;
import com.forum.pojo.forum.ForumUser;
import com.forum.pojo.talk.TalkPosts;
import com.forum.pojo.talk.TalkPostsComment;
import com.forum.pojo.talk.TalkPostsCommentReply;
import com.forum.service.other.ThreadService;
import com.forum.service.talk.TalkPostsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Tsing
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TalkPostsServiceImpl extends ServiceImpl<TalkPostsMapper, TalkPosts> implements TalkPostsService {

    private final TalkPostsMapper postsMapper;
    private final ForumUserMapper userMapper;
    private final TalkPostsCommentMapper talkPostsCommentMapper;
    private final TalkPostsCommentReplyMapper replyMapper;

    @Override
    public ApiResult<String> createPosts(TalkPosts posts) {
        Integer currentUserId = LocalContext.getCurrentUserId();
        posts.setPublisherId(currentUserId);
        postsMapper.insert(posts);
        return ApiResult.success();
    }

    @Override
    public ApiResult<Map<String, Object>> getPostsByTalk(PostsQueryParam queryParam) {
        LambdaQueryWrapper<TalkPosts> queryWrapper = new LambdaQueryWrapper<>();
        Page<TalkPosts> page = new Page<>();
        page.setCurrent(queryParam.getPage()).setSize(queryParam.getSize());
        queryWrapper.eq(TalkPosts::getTalkId,queryParam.getTalkId());

        Page<TalkPosts> postsPage = postsMapper.selectPage(page, queryWrapper);
        List<TalkPosts> records = postsPage.getRecords();

        List<PostsInfoVo> collect = records.stream().map(item -> {

            //获取评论总数(含评论的回复)
            int commentNum=0;
            LambdaQueryWrapper<TalkPostsComment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            commentLambdaQueryWrapper.eq(TalkPostsComment::getPostsId,item.getId());
            List<TalkPostsComment> talkPostsComments = talkPostsCommentMapper.selectList(commentLambdaQueryWrapper);
            commentNum+=talkPostsComments.size();
            for (TalkPostsComment comment:talkPostsComments) {
                LambdaQueryWrapper<TalkPostsCommentReply> replyLambdaQueryWrapper = new LambdaQueryWrapper<>();
                replyLambdaQueryWrapper.eq(TalkPostsCommentReply::getCommentId,comment.getId());
                List<TalkPostsCommentReply> talkPostsCommentReplies = replyMapper.selectList(replyLambdaQueryWrapper);
                commentNum+=talkPostsCommentReplies.size();
            }

            ForumUser forumUser = userMapper.selectById(item.getPublisherId());
            PostsInfoVo postsInfoVo = new PostsInfoVo(forumUser.getNickname(),forumUser.getAvatar(),commentNum);
            BeanUtils.copyProperties(item,postsInfoVo);
            return postsInfoVo;
        }).collect(Collectors.toList());

        Map<String,Object> map = new HashMap<>(16);
        map.put("total",postsPage.getTotal());
        map.put("list",collect);

        return ApiResult.success(map);
    }

    @Override
    public ApiResult<PostsDetailVo> getPostDetail(Integer postId) {

        PostsDetailVo postsDetailVo = new PostsDetailVo();


        //获取帖子详情
        TalkPosts talkPosts = postsMapper.selectById(postId);
        BeanUtils.copyProperties(talkPosts,postsDetailVo);
        //获取帖子的发布者昵称
        ForumUser postsUser = userMapper.selectById(talkPosts.getPublisherId());
        postsDetailVo.setPublisherName(postsUser.getNickname());


        //获取评论 截取十条
        LambdaQueryWrapper<TalkPostsComment> commentWrapper = new LambdaQueryWrapper<>();
        commentWrapper.eq(TalkPostsComment::getPostsId,talkPosts.getId()).last("limit 10");
        List<TalkPostsComment> talkPostsComments = talkPostsCommentMapper.selectList(commentWrapper);
            //获取该评论下的头像和发布者姓名 以及该评论的回复
        List<TalkPostCommentInfoVo> commentInfoVos = talkPostsComments.stream().map(postComment -> {

                //获取评论发布者的昵称和头像
            ForumUser forumUser = userMapper.selectById(postComment.getPublisherId());
            TalkPostCommentInfoVo talkPostCommentInfoVo = new TalkPostCommentInfoVo(forumUser.getNickname(),forumUser.getAvatar());
            BeanUtils.copyProperties(postComment,talkPostCommentInfoVo);

                //获取该评论下的回复 截取十条
            LambdaQueryWrapper<TalkPostsCommentReply> replyWrapper = new LambdaQueryWrapper<>();
            replyWrapper.eq(TalkPostsCommentReply::getCommentId,postComment.getId()).last("limit 10");
            List<TalkPostsCommentReply> talkPostsCommentReplies = replyMapper.selectList(replyWrapper);
                //获取每条评论回复的用户头像和昵称
            List<TalkPostsReplyInfoVo> replyInfoVos = talkPostsCommentReplies.stream().map(reply -> {
                TalkPostsReplyInfoVo replyInfoVo = new TalkPostsReplyInfoVo();
                BeanUtils.copyProperties(reply,replyInfoVo);

                    //获取回复的发送者信息
                ForumUser replyUser = userMapper.selectById(reply.getPublisherId());
                replyInfoVo.setNickname(replyUser.getNickname());
                replyInfoVo.setAvatar(replyUser.getAvatar());
                    //获取被回复者的信息
                ForumUser parentUser = userMapper.selectById(reply.getParentUserId());
                replyInfoVo.setParentNickname(parentUser.getNickname());
                return replyInfoVo;
            }).collect(Collectors.toList());
            //获取该评论下的十条回复
            talkPostCommentInfoVo.setReplyInfoVos(replyInfoVos);
            return talkPostCommentInfoVo;
        }).collect(Collectors.toList());

        postsDetailVo.setCommentInfoVos(commentInfoVos);
        return ApiResult.success(postsDetailVo);
    }

    @Override
    public ApiResult<List<RecommendTalkInfoVo>> getRecommendPost() {
        LambdaQueryWrapper<TalkPosts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TalkPosts::getIsHot,1);
        List<TalkPosts> talkPosts = postsMapper.selectList(queryWrapper);

        List<RecommendTalkInfoVo> recommendTalkInfoVos = talkPosts.stream().map(item -> {
            RecommendTalkInfoVo recommendTalkInfoVo = new RecommendTalkInfoVo();
            BeanUtils.copyProperties(item,recommendTalkInfoVo);

            //获取评论总数
            Long commentCount = talkPostsCommentMapper.commentCount(item.getId());
            recommendTalkInfoVo.setCommentCount(commentCount);

            //获取发布者昵称
            ForumUser forumUser = userMapper.selectById(item.getPublisherId());
            recommendTalkInfoVo.setNickname(forumUser.getNickname());
            recommendTalkInfoVo.setAvatar(forumUser.getAvatar());

            return recommendTalkInfoVo;
        }).collect(Collectors.toList());

        return ApiResult.success(recommendTalkInfoVos);
    }
}
